package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class GaborActivity2 extends Activity {
	String analysisorigin = "";

	ImageView ivImage;
	public Bitmap originalBmp; // origin
	public Bitmap activeBmp; // edited image
	public Bitmap gaborBmp;
	public Bitmap takenBmp;
	Button btPrev, btSegment, btSave, btFilter, btInverse;
	TextView tvThreshold, tvLambda, tvSigma;
	SeekBar sbThreshold, sbLambda, sbSigma;

	EditText etSigma;
	EditText etLambda;
	EditText etThreshold;

	int kernelSize = 24;
	//int kernelSize = 12;
	// double sigma = 8;
	double sigma = 0;
	double theta = Math.PI / 2;
	// double lambda = 21;
	double lambda = 0;
	double gamma = 1;
	double psi = 1.5;

	public static int inverse = 1;
	public static int currentprogress;
	public static int thresholdvalue;
	public static int sigmavalue;
	public static int lambdavalue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gaboractivity2);

		if (getIntent().hasExtra("analysisorigin")) {
			analysisorigin = getIntent().getStringExtra("analysisorigin");
		}

		LinearLayout myLayout = (LinearLayout) findViewById(R.id.gaboractivity2);
		myLayout.requestFocus();

		ivImage = (ImageView) findViewById(R.id.ivImage);
		// byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		// originalBmp = BitmapFactory.decodeByteArray(byteArray, 0,
		// byteArray.length);

		try {
			originalBmp = InternalStorage.readBitmap(this, "gaussianBmp");
			activeBmp = originalBmp.copy(Config.ARGB_8888, true);

			//Bitmap resizedbmp = Bitmap.createScaledBitmap(originalBmp, 300,
			//		300, true);
			//ivImage.setImageBitmap(resizedbmp);
			ivImage.setImageBitmap(activeBmp);
			btPrev = (Button) findViewById(R.id.btPrev);
			btSegment = (Button) findViewById(R.id.btSegment);

			btSave = (Button) findViewById(R.id.btSave);
			// btFilter = (Button) findViewById(R.id.btFilter);
			// tvThreshold = (TextView) findViewById(R.id.tvThreshold);

			sbThreshold = (SeekBar) findViewById(R.id.sbThreshold);
			// apply gabor before threshold
			//sbThreshold.setEnabled(false);
			btPrev.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					startActivity(new Intent(GaborActivity2.this,
							GaussianActivity.class));

				}
			});
			btSegment.setEnabled(false);
			btSegment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (analysisorigin.equals("Protein")) {
						Intent goToNextActivity = new Intent(
								getApplicationContext(),
								SegmentationActivity.class);
						byte[] bytes;
						if (activeBmp == null) {
							bytes = compressImage(originalBmp);
						} else {
							bytes = compressImage(activeBmp);
						}
						goToNextActivity.putExtra("BMP", bytes);
						goToNextActivity.putExtra("analysisorigin", analysisorigin);
						startActivity(goToNextActivity);

					}
					else
					{
						Intent goToNextActivity = new Intent(
								getApplicationContext(),
								DNASegmentationActivity.class);
						byte[] bytes;
						if (activeBmp == null) {
							bytes = compressImage(originalBmp);
						} else {
							bytes = compressImage(activeBmp);
						}
						goToNextActivity.putExtra("BMP", bytes);
						goToNextActivity.putExtra("analysisorigin", analysisorigin);
						startActivity(goToNextActivity);
					}
				}
			});

			etThreshold = (EditText) findViewById(R.id.etThreshold);
			//etThreshold.setEnabled(false);
			etThreshold.setFilters(new InputFilter[] { new InputFilterMinMax(
					"0", "255") });
			etThreshold.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
					int textLength = etThreshold.getText().length();
					etThreshold.setSelection(textLength, textLength);
					if (gaborBmp == null) {
						if (takenBmp == null) {
							takenBmp = originalBmp;
						}
					} else {
						// if (takenBmp == null) {
						takenBmp = gaborBmp;
						// }
					}

					int textLength1 = etThreshold.getText().length();
					etThreshold.setSelection(textLength1, textLength1);

					if (etThreshold.getText().toString().equals("")) {
						currentprogress = 0;
					} else {
						currentprogress = Integer.parseInt(etThreshold
								.getText().toString());
					}

					// currentprogress = sbThreshold.getProgress();
					thresholdvalue = currentprogress;
					if (currentprogress != 0) {
						Mat srcImg = new Mat(takenBmp.getHeight(), takenBmp
								.getWidth(), CvType.CV_8U, new Scalar(4));
						Bitmap myBitmap32 = takenBmp.copy(
								Bitmap.Config.ARGB_8888, true);
						Utils.bitmapToMat(myBitmap32, srcImg);
						Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
						// Mat thresholdMat = new Mat();
						Mat thresholdMat = new Mat(takenBmp.getHeight(),
								takenBmp.getWidth(), CvType.CV_8U,
								new Scalar(4));
						/*
						if (inverse == 1) {
							Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
							Imgproc.threshold(srcImg, thresholdMat,
									thresholdvalue, 255,
									Imgproc.THRESH_BINARY_INV);
							
							inverse = 1;
						} else {
							Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
							Imgproc.threshold(srcImg, thresholdMat,
									thresholdvalue, 255, Imgproc.THRESH_BINARY);
							inverse = 0;
						}
						*/
						// invertedThreshold(srcImg,
						// thresholdMat,thresholdvalue,255);
						
						
						Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
						Imgproc.threshold(srcImg, thresholdMat,
								thresholdvalue, 255, Imgproc.THRESH_BINARY);
						
						Bitmap thBmp = Bitmap.createBitmap(thresholdMat.cols(),
								thresholdMat.rows(), Bitmap.Config.ARGB_8888);
						Utils.matToBitmap(thresholdMat, thBmp);
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(thBmp,
						//		300, 300, true);
						activeBmp = thBmp;
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						btInverse.setEnabled(true);
						sbThreshold.setProgress(thresholdvalue);
						
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						// tvThreshold.setText("Threshold: " + currentprogress);
						sbThreshold.setProgress(thresholdvalue);
					}
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});

			sbThreshold
					.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

						@Override
						public void onStopTrackingTouch(SeekBar seekBar) {

						}

						@Override
						public void onStartTrackingTouch(SeekBar seekBar) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onProgressChanged(SeekBar seekBar,
								int progress, boolean fromUser) {

							if (gaborBmp == null) {
								if (takenBmp == null) {
									takenBmp = originalBmp;
								}
							} else {
								// if (takenBmp == null) {
								takenBmp = gaborBmp;
								// }
							}

							currentprogress = sbThreshold.getProgress();
							thresholdvalue = currentprogress;
							if (currentprogress != 0) {
								Mat srcImg = new Mat(takenBmp.getHeight(),
										takenBmp.getWidth(), CvType.CV_8U,
										new Scalar(4));
								Bitmap myBitmap32 = takenBmp.copy(
										Bitmap.Config.ARGB_8888, true);
								Utils.bitmapToMat(myBitmap32, srcImg);
								Imgproc.cvtColor(srcImg, srcImg,
										Imgproc.COLOR_BGR2GRAY);
								// Mat thresholdMat = new Mat();
								Mat thresholdMat = new Mat(
										takenBmp.getHeight(), takenBmp
												.getWidth(), CvType.CV_8U,
										new Scalar(4));
								/*
								if (inverse == 1) {
									
									Imgproc.threshold(srcImg, thresholdMat,
											thresholdvalue, 255,
											Imgproc.THRESH_BINARY_INV);
									inverse = 1;
								} else {
									
									Imgproc.threshold(srcImg, thresholdMat,
											thresholdvalue, 255,
											Imgproc.THRESH_BINARY);
									inverse = 0;
								}
								*/
								// invertedThreshold(srcImg,
								// thresholdMat,thresholdvalue,255);
								
								Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
								Imgproc.threshold(srcImg, thresholdMat,
										thresholdvalue, 255, Imgproc.THRESH_BINARY);
								
								Bitmap thBmp = Bitmap.createBitmap(
										thresholdMat.cols(),
										thresholdMat.rows(),
										Bitmap.Config.ARGB_8888);
								Utils.matToBitmap(thresholdMat, thBmp);
								//Bitmap resizedbmp = Bitmap.createScaledBitmap(
								//		thBmp, 300, 300, true);
								activeBmp = thBmp;
								//ivImage.setImageBitmap(resizedbmp);
								ivImage.setImageBitmap(activeBmp);
								btInverse.setEnabled(true);
								etThreshold.setText("" + thresholdvalue);
								
							} else {
								//Bitmap resizedbmp = Bitmap.createScaledBitmap(
								//		activeBmp, 300, 300, true);
								//ivImage.setImageBitmap(resizedbmp);
								//ivImage.setImageBitmap(activeBmp);
								ivImage.setImageBitmap(gaborBmp);
								// tvThreshold.setText("Threshold: " +
								// currentprogress);
								
								
								etThreshold.setText("" + thresholdvalue);
							}
						}
					});

			etLambda = (EditText) findViewById(R.id.etLambda);
			etLambda.setFilters(new InputFilter[] { new InputFilterMinMax("0",
					"100") });
			etLambda.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
					int textLength = etLambda.getText().length();
					etLambda.setSelection(textLength, textLength);
					if (etLambda.getText().toString().equals("")) {
						currentprogress = 0;
					} else {
						currentprogress = Integer.parseInt(etLambda.getText()
								.toString());
					}

					lambdavalue = currentprogress;
					if (currentprogress != 0) {
						lambda = lambdavalue;
						Mat filtermat = new Mat(originalBmp.getHeight(),
								originalBmp.getWidth(), CvType.CV_8U);
						int ch = filtermat.channels();
						Utils.bitmapToMat(originalBmp, filtermat);
						Imgproc.cvtColor(filtermat, filtermat,
								Imgproc.COLOR_BGR2GRAY);

						ch = filtermat.channels();
						// result image below
						Mat resultImg = new Mat();
						// set gabor
						Mat gaborKernel = Imgproc.getGaborKernel(new Size(
								kernelSize, kernelSize), sigma,
								theta/* horizontal */, lambda, gamma, psi,
								CvType.CV_32F);

						ch = filtermat.channels();
						Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);

						ch = resultImg.channels();

						Bitmap filteredBmp = Bitmap.createBitmap(
								resultImg.cols(), resultImg.rows(),
								Bitmap.Config.ARGB_8888);
						Utils.matToBitmap(resultImg, filteredBmp);

						gaborBmp = filteredBmp;
						activeBmp = filteredBmp;
						// display result
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		filteredBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						
						// apply gabor before threshold
				
						sbLambda.setProgress(lambdavalue);
						sbThreshold.setEnabled(true);
						btSegment.setEnabled(true);
						etThreshold.setEnabled(true);
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		activeBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						//ivImage.setImageBitmap(activeBmp);
						activeBmp = originalBmp;
						ivImage.setImageBitmap(originalBmp);
						sbLambda.setProgress(lambdavalue);
					}

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});
			// tvLambda = (TextView) findViewById(R.id.tvLambda);
			sbLambda = (SeekBar) findViewById(R.id.sbLambda);
			// sbLambda.setProgress(21);
			sbLambda.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					// TODO Auto-generated method stub
					/*
					 * if (activeBmp == null) { if (takenBmp == null) { takenBmp
					 * = originalBmp; } } else { if (takenBmp == null) {
					 * takenBmp = activeBmp; } }
					 */
					currentprogress = sbLambda.getProgress();
					lambdavalue = currentprogress;
					if (currentprogress != 0) {
						lambda = lambdavalue;
						Mat filtermat = new Mat(originalBmp.getHeight(),
								originalBmp.getWidth(), CvType.CV_8U);
						int ch = filtermat.channels();
						Utils.bitmapToMat(originalBmp, filtermat);
						Imgproc.cvtColor(filtermat, filtermat,
								Imgproc.COLOR_BGR2GRAY);

						ch = filtermat.channels();
						// result image below
						Mat resultImg = new Mat();
						// set gabor
						Mat gaborKernel = Imgproc.getGaborKernel(new Size(
								kernelSize, kernelSize), sigma,
								theta/* horizontal */, lambda, gamma, psi,
								CvType.CV_32F);

						ch = filtermat.channels();
						Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);

						ch = resultImg.channels();

						Bitmap filteredBmp = Bitmap.createBitmap(
								resultImg.cols(), resultImg.rows(),
								Bitmap.Config.ARGB_8888);
						Utils.matToBitmap(resultImg, filteredBmp);

						gaborBmp = filteredBmp;
						activeBmp = filteredBmp;
						// display result
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		filteredBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						// apply gabor before threshold
						
						etLambda.setText("" + lambdavalue);
						sbThreshold.setEnabled(true);
						// btInverse.setEnabled(true);
						btSegment.setEnabled(true);
						etThreshold.setEnabled(true);
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		originalBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						//ivImage.setImageBitmap(activeBmp);
						activeBmp = originalBmp;
						ivImage.setImageBitmap(originalBmp);
						// tvLambda.setText("Lambda: " + currentprogress);
						etLambda.setText("" + lambdavalue);
					}
				}
			});

			etSigma = (EditText) findViewById(R.id.etSigma);
			etSigma.setFilters(new InputFilter[] { new InputFilterMinMax("0",
					"100") });
			etSigma.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
					int textLength = etSigma.getText().length();
					etSigma.setSelection(textLength, textLength);
					if (etSigma.getText().toString().equals("")) {
						currentprogress = 0;
					} else {
						currentprogress = Integer.parseInt(etSigma.getText()
								.toString());
					}

					sigmavalue = currentprogress;
					if (currentprogress != 0) {
						sigma = sigmavalue;
						Mat filtermat = new Mat(originalBmp.getHeight(),
								originalBmp.getWidth(), CvType.CV_8U);
						int ch = filtermat.channels();
						Utils.bitmapToMat(originalBmp, filtermat);
						Imgproc.cvtColor(filtermat, filtermat,
								Imgproc.COLOR_BGR2GRAY);

						ch = filtermat.channels();
						// result image below
						Mat resultImg = new Mat();
						// set gabor
						Mat gaborKernel = Imgproc.getGaborKernel(new Size(
								kernelSize, kernelSize), sigma,
								theta/* horizontal */, lambda, gamma, psi,
								CvType.CV_32F);

						ch = filtermat.channels();
						Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);

						ch = resultImg.channels();

						Bitmap filteredBmp = Bitmap.createBitmap(
								resultImg.cols(), resultImg.rows(),
								Bitmap.Config.ARGB_8888);
						Utils.matToBitmap(resultImg, filteredBmp);

						gaborBmp = filteredBmp;
						activeBmp = filteredBmp;
						// display result
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		filteredBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						// apply gabor before threshold
						
						sbSigma.setProgress(sigmavalue);
						sbThreshold.setEnabled(true);
						// btInverse.setEnabled(true);
						btSegment.setEnabled(true);
						etThreshold.setEnabled(true);
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		originalBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						//ivImage.setImageBitmap(activeBmp);
						activeBmp = originalBmp;
						ivImage.setImageBitmap(originalBmp);
						// tvSigma.setText("Lambda: " + currentprogress);
						sbSigma.setProgress(sigmavalue);
					}

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});

			// tvSigma = (TextView) findViewById(R.id.tvSigma);
			sbSigma = (SeekBar) findViewById(R.id.sbSigma);
			// sbLambda.setProgress(8);
			sbSigma.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					// TODO Auto-generated method stub
					/*
					 * if (activeBmp == null) { if (takenBmp == null) { takenBmp
					 * = originalBmp; } } else { if (takenBmp == null) {
					 * takenBmp = activeBmp; } }
					 */
					currentprogress = sbSigma.getProgress();
					sigmavalue = currentprogress;
					if (currentprogress != 0) {
						sigma = sigmavalue;
						Mat filtermat = new Mat(originalBmp.getHeight(),
								originalBmp.getWidth(), CvType.CV_8U);
						int ch = filtermat.channels();
						Utils.bitmapToMat(originalBmp, filtermat);
						Imgproc.cvtColor(filtermat, filtermat,
								Imgproc.COLOR_BGR2GRAY);

						ch = filtermat.channels();
						// result image below
						Mat resultImg = new Mat();
						// set gabor
						Mat gaborKernel = Imgproc.getGaborKernel(new Size(
								kernelSize, kernelSize), sigma,
								theta/* horizontal */, lambda, gamma, psi,
								CvType.CV_32F);

						ch = filtermat.channels();
						Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);

						ch = resultImg.channels();

						Bitmap filteredBmp = Bitmap.createBitmap(
								resultImg.cols(), resultImg.rows(),
								Bitmap.Config.ARGB_8888);
						Utils.matToBitmap(resultImg, filteredBmp);

						gaborBmp = filteredBmp;
						activeBmp = filteredBmp;
						// display result
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		filteredBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						// apply gabor before threshold
						// tvSigma.setText("Sigma: " + currentprogress);
						etSigma.setText("" + sigmavalue);
						sbThreshold.setEnabled(true);
						// btInverse.setEnabled(true);
						btSegment.setEnabled(true);
						etThreshold.setEnabled(true);
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(
						//		originalBmp, 300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						//ivImage.setImageBitmap(activeBmp);
						// tvSigma.setText("Lambda: " + currentprogress);
						activeBmp = originalBmp;
						ivImage.setImageBitmap(originalBmp);
						etSigma.setText("" + sigmavalue);
					}
				}
			});
			btInverse = (Button) findViewById(R.id.btInverse);
			btInverse.setEnabled(false);
			btInverse.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (inverse == 1) {
						inverse = 0;

					} else {
						inverse = 1;
					}
					if (takenBmp == null) {
						takenBmp = originalBmp;
					}

					Mat srcImg = new Mat(takenBmp.getHeight(), takenBmp
							.getWidth(), CvType.CV_8U, new Scalar(4));
					Bitmap myBitmap32 = takenBmp.copy(Bitmap.Config.ARGB_8888,
							true);
					Utils.bitmapToMat(myBitmap32, srcImg);
					Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
					// Mat thresholdMat = new Mat();
					Mat thresholdMat = new Mat(takenBmp.getHeight(), takenBmp
							.getWidth(), CvType.CV_8U, new Scalar(4));
					if (inverse == 1) {
						Imgproc.threshold(srcImg, thresholdMat, thresholdvalue,
								255, Imgproc.THRESH_BINARY_INV);
					} else {
						Imgproc.threshold(srcImg, thresholdMat, thresholdvalue,
								255, Imgproc.THRESH_BINARY);
					}
					// invertedThreshold(srcImg,
					// thresholdMat,thresholdvalue,255);
					Bitmap thBmp = Bitmap.createBitmap(thresholdMat.cols(),
							thresholdMat.rows(), Bitmap.Config.ARGB_8888);
					Utils.matToBitmap(thresholdMat, thBmp);
					activeBmp = thBmp;
					//Bitmap resizedbmp = Bitmap.createScaledBitmap(thBmp, 300,
					//		300, true);

					//ivImage.setImageBitmap(resizedbmp);
					ivImage.setImageBitmap(activeBmp);
					
				}
			});
			Button btRecommendedProtein = (Button)findViewById(R.id.btProtein);
			if(analysisorigin.equals("Protein")){
				btRecommendedProtein.setVisibility(View.VISIBLE);
			}
			btRecommendedProtein.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					sbSigma.setProgress(28);
					sbLambda.setProgress(28);
					sbThreshold.setProgress(55);
					etSigma.setText(""+28);
					etLambda.setText(""+28);
					etThreshold.setText(""+55);
					
					sigmavalue = 28;
					lambdavalue = 28;
					thresholdvalue = 28;
					sigma = 28;
					lambda = 28;
					
					Mat filtermat = new Mat(originalBmp.getHeight(),
							originalBmp.getWidth(), CvType.CV_8U);
					Utils.bitmapToMat(originalBmp, filtermat);
					Imgproc.cvtColor(filtermat, filtermat,
							Imgproc.COLOR_BGR2GRAY);
					// result image below
					Mat resultImg = new Mat();
					// set gabor
					Mat gaborKernel = Imgproc.getGaborKernel(new Size(
							kernelSize, kernelSize), sigma,
							theta/* horizontal */, lambda, gamma, psi,
							CvType.CV_32F);
					Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);
					Bitmap filteredBmp = Bitmap.createBitmap(
							resultImg.cols(), resultImg.rows(),
							Bitmap.Config.ARGB_8888);
					Utils.matToBitmap(resultImg, filteredBmp);
					gaborBmp = filteredBmp;
					activeBmp = filteredBmp;
					if (gaborBmp == null) {
						if (takenBmp == null) {
							takenBmp = originalBmp;
						}
					} else {
						takenBmp = gaborBmp;
					}
					Mat srcImg = new Mat(takenBmp.getHeight(), takenBmp
							.getWidth(), CvType.CV_8U, new Scalar(4));
					Bitmap myBitmap32 = takenBmp.copy(
							Bitmap.Config.ARGB_8888, true);
					Utils.bitmapToMat(myBitmap32, srcImg);
					Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
					Mat thresholdMat = new Mat(takenBmp.getHeight(),
							takenBmp.getWidth(), CvType.CV_8U,
							new Scalar(4));
					if (inverse == 1) {
						Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
						Imgproc.threshold(srcImg, thresholdMat,
								thresholdvalue, 255,
								Imgproc.THRESH_BINARY_INV);
						
						inverse = 1;
					} else {
						Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
						Imgproc.threshold(srcImg, thresholdMat,
								thresholdvalue, 255, Imgproc.THRESH_BINARY);
						inverse = 0;
					}
					Bitmap thBmp = Bitmap.createBitmap(thresholdMat.cols(),
							thresholdMat.rows(), Bitmap.Config.ARGB_8888);
					Utils.matToBitmap(thresholdMat, thBmp);
					activeBmp = thBmp;
					ivImage.setImageBitmap(activeBmp);
					btInverse.setEnabled(true);
					etThreshold.setEnabled(true);
					sbThreshold.setEnabled(true);
					
					
				}
			});
			
			Button btRecommendedDNA = (Button)findViewById(R.id.btDNA);
			if(analysisorigin.equals("DNA")){
				btRecommendedDNA.setVisibility(View.VISIBLE);
			}
			btRecommendedDNA.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					sbSigma.setProgress(3);
					sbLambda.setProgress(11);
					sbThreshold.setProgress(240);
					etSigma.setText(""+3);
					etLambda.setText(""+11);
					etThreshold.setText(""+240);
					
					sigmavalue = 3;
					lambdavalue = 11;
					thresholdvalue = 240;
					sigma = 3;
					lambda = 11;
					
					Mat filtermat = new Mat(originalBmp.getHeight(),
							originalBmp.getWidth(), CvType.CV_8U);
					Utils.bitmapToMat(originalBmp, filtermat);
					Imgproc.cvtColor(filtermat, filtermat,
							Imgproc.COLOR_BGR2GRAY);
					// result image below
					Mat resultImg = new Mat();
					// set gabor
					Mat gaborKernel = Imgproc.getGaborKernel(new Size(
							kernelSize, kernelSize), sigma,
							theta/* horizontal */, lambda, gamma, psi,
							CvType.CV_32F);
					Imgproc.filter2D(filtermat, resultImg, -1, gaborKernel);
					Bitmap filteredBmp = Bitmap.createBitmap(
							resultImg.cols(), resultImg.rows(),
							Bitmap.Config.ARGB_8888);
					Utils.matToBitmap(resultImg, filteredBmp);
					gaborBmp = filteredBmp;
					activeBmp = filteredBmp;
					if (gaborBmp == null) {
						if (takenBmp == null) {
							takenBmp = originalBmp;
						}
					} else {
						takenBmp = gaborBmp;
					}
					Mat srcImg = new Mat(takenBmp.getHeight(), takenBmp
							.getWidth(), CvType.CV_8U, new Scalar(4));
					Bitmap myBitmap32 = takenBmp.copy(
							Bitmap.Config.ARGB_8888, true);
					Utils.bitmapToMat(myBitmap32, srcImg);
					Imgproc.cvtColor(srcImg, srcImg, Imgproc.COLOR_BGR2GRAY);
					Mat thresholdMat = new Mat(takenBmp.getHeight(),
							takenBmp.getWidth(), CvType.CV_8U,
							new Scalar(4));
					if (inverse == 1) {
						Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
						Imgproc.threshold(srcImg, thresholdMat,
								thresholdvalue, 255,
								Imgproc.THRESH_BINARY_INV);
						
						inverse = 1;
					} else {
						Imgproc.dilate(srcImg, srcImg, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
						Imgproc.threshold(srcImg, thresholdMat,
								thresholdvalue, 255, Imgproc.THRESH_BINARY);
						inverse = 0;
					}
					Bitmap thBmp = Bitmap.createBitmap(thresholdMat.cols(),
							thresholdMat.rows(), Bitmap.Config.ARGB_8888);
					Utils.matToBitmap(thresholdMat, thBmp);
					activeBmp = thBmp;
					ivImage.setImageBitmap(activeBmp);
					btInverse.setEnabled(true);
					etThreshold.setEnabled(true);
					sbThreshold.setEnabled(true);
					
					
				}
			});
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;

	}

	public void invertedThreshold(Mat srcImg, Mat destImg, double thresh,
			double maxval) {
		for (int i = 0; i < srcImg.height(); i++) {
			for (int j = 0; j < srcImg.width(); j++) {
				double[] data = srcImg.get(i, j);
				if (data[0] > thresh)
					destImg.put(i, j, 0);
				else
					destImg.put(i, j, maxval);
			}
		}
	}
}
